package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.bean.OrderEntity;
import com.atguigu.springcloud.alibaba.feign.AccountFeign;
import com.atguigu.springcloud.alibaba.feign.StorageFeign;
import com.atguigu.springcloud.alibaba.mapper.OrderMapper;
import com.atguigu.springcloud.alibaba.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author timiv
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource(type = OrderMapper.class)
    private OrderMapper orderMapper;
    @Resource
    private AccountFeign accountFeign;
    @Resource
    private StorageFeign storageFeign;

    @Override
    @GlobalTransactional
    public void saveOrder(OrderEntity entity) {
        //下订单
        orderMapper.save(entity);
        //减金额
        accountFeign.subMoney(entity.getUserId(),entity.getMoney());
        //扣库存
        storageFeign.subStorage(entity.getProductId(),entity.getCount());
        //该状态
        entity.setStatus(0);
        orderMapper.updateStatus(entity);
    }
}
